<template>
	<view class="">
		<view class="" id="topBox">
			<HeadersIcon></HeadersIcon>
			<template v-if="!isUnauthorized">
				<u-line></u-line>
				<search @choose="chooseSearch" :data="scrollData" page="stock"></search>
				<u-line></u-line>
			</template>
		</view>
		<template v-if="isUnauthorized">
			<unauthorized></unauthorized>
		</template>
		<template v-if="scrollHeight && loadtime">
			<stockMaterialList :data="scrollData" :menu="scrollMenu" :height="scrollHeight" :scroll_id="scrollViewId"
				:isedit="false" :loadtime="loadtime" @scrollViewIdDone="scrollViewIdDone" :init_value="[]" page="stock">
			</stockMaterialList>
		</template>
	</view>
</template>

<script>
	import HeadersIcon from "./components/HeadersIcon.vue";
	import stockMaterialList from "@/components/stock-material-list/stock-material-list.vue";
	import search from "@/components/search.vue";
	import unauthorized from "@/components/unauthorized.vue";
	import {
		getStock,
		stock_change
	} from "@c/api.js";

	export default {
		components: {
			HeadersIcon,
			stockMaterialList,
			search,
			unauthorized
		},
		data() {
			return {
				role: {},
				scrollViewId: 0,
				scrollHeight: '',
				scrollData: [],
				scrollMenu: [],
				loadtime: '',
				isUnauthorized: false,
			};
		},
		methods: {
			chooseSearch(id) {
				this.scrollViewId = id;
			},
			scrollViewIdDone() {
				this.scrollViewId = 0;
			},
			// 获取仓库数据
			async getData() {
				uni.showLoading({
					title: "加载中",
				});
				let stock = this.$store.getters.get_stock;
				if (stock.length == 0) {
					try {
						await this.getStock();
						stock = this.$store.getters.get_stock;
					} catch (e) {
						uni.$u.toast(e || '错误');
						uni.stopPullDownRefresh();
						return;
					}
				}
				// console.log(stock);
				stock_change()
					.then((res) => {
						let {update,unlink} = res.result;
						let update_value = {
							updateArr:update,
							delIds:unlink
						};
						this.$store.commit('update_stock', update_value);
						stock = this.$store.getters.get_stock;
						this.scrollData = stock;
						this.scrollMenu = res.result['库位'] || [];
						this.loadtime = new Date().getTime();
					})
					.finally(() => {
						uni.hideLoading();
						uni.stopPullDownRefresh();
					});
			},
			async getStock() {
				await getStock().then((res) => {
					this.$store.commit('set_stock', res.result['库存']);
				}).catch(() => {
					throw '缓存失败 getStock';
				}).finally(() => {

				})
			}
		},

		onLoad(d) {
			try {
				this.role = this.$store.getters.get_role;
				this.stockAuth = this.role['仓管模块'];
				this.isUnauthorized = !this.stockAuth['仓管'].write;
				if (!this.isUnauthorized) {
					this.getData();
				}
			} catch (e) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
			}
		},
		onShow() {
			uni.$off('refresh_stock');
			uni.$once('refresh_stock', () => {
				if (!this.isUnauthorized) {
					this.getData();
				}
			})
		},
		onReady() {
			uni.createSelectorQuery().select('#topBox').boundingClientRect(data => {
				this.scrollHeight = this.getClineHeight() - data.height;
			}).exec()
		},
		onPullDownRefresh() {
			if (!this.isUnauthorized) {
				this.getData();
			}
		},
	};
</script>

<style lang="scss" scoped>

</style>